export const setLocalData = (name, value) => {
	if (!value) {
		return;
	}
	if (typeof value !== "string") {
		value = JSON.stringify(value);
	}
	window.sessionStorage.setItem(name, value);
}

export const getLocalData = name => {
	if (!name) {
		return;
	}
	let data = window.sessionStorage.getItem(name);
	try{
		data = JSON.parse(data);
	}catch(e){
		//TODO handle the exception
	}
	
	return data;
}

export const removeLocalData = name => {
	if (!name) {
		return;
	}
	window.sessionStorage.removeItem(name);
}

export const getHashCode = data => {
	let str = data;
	if (typeof(str) == "object") {
		str = JSON.stringify(str)
	}
	var hash = 0;
	var i, chr, len;
	if (str.length === 0) return hash;
	for (i = 0, len = str.length; i < len; i++) {
		chr = str.charCodeAt(i);
		hash = ((hash << 5) - hash) + chr;
		hash |= 0; // Convert to 32bit integer
	}
	return hash;
}

export const validatePhone = (rule, value, callback) => {
	var phonePattern = /^1[23456789]\d{9}$/;
	if (phonePattern.test(value)) {
		callback();
	} else {
		return callback(new Error('请输入正确的手机号'));
	}
}

export const toTimeStr= (time,isDay) =>{
	let format;
	var Y = time.getFullYear()
	var M = (time.getMonth() + 1).toString().padStart(2, '0')
	var D = time.getDate().toString().padStart(2, '0')
	var h = time.getHours().toString().padStart(2, '0')
	var m = time.getMinutes().toString().padStart(2, '0')
	var s = time.getSeconds().toString().padStart(2, '0')
	if(isDay){
		format = `${Y}-${M}-${D}`
	}else{
		format = `${Y}-${M}-${D} ${h}:${m}:${s}`
	}
	return format
}

extendArrayFunction();

function extendArrayFunction() {
	//数组功能扩展
	//两个数组的交集
	Array.intersect = function(a, b) {
		let newA = [...new Set(a)];
		let l = newA.filter(el => {
			return b.includes(el);
		})
		return l;
	};
	//两个数组的差集
	Array.minus = function(a, b) {
		let newA = [...new Set(a)];
		let l = newA.filter(el => {
			return !(b.includes(el));
		})
		return l;
	};
	//两个数组的补集
	Array.complement = function(a, b) {
		return Array.minus(Array.union(a, b), Array.intersect(a, b));
	};
	//两个数组并集
	Array.union = function(a, b) {
		return [...new Set(a.concat(b))];
	};
	//a数组是否包含b数组
	Array.isContain = function(a,b){
		if(!a || !b){
			return false;
		}
		for(let i = 0 ; i < b.length ; i++){
			if(!a.includes(b[i])){
				return false
			}
		}
		return true;
	}
}

export const getUUID = (len, radix)=>{
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	var uuid = [],
		i;
	radix = radix || chars.length;

	if (len) {
		// Compact form
		for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		// rfc4122, version 4 form
		var r;

		// rfc4122 requires these characters
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';

		// Fill in random data.  At i==19 set the high bits of clock sequence as
		// per rfc4122, sec. 4.1.5
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}
	return uuid.join('');
}